import logging
import os


class ComLog:
    def __init__(self, cls_name):
        log_path = os.path.join(os.getcwd(), 'log', 'run.log')
        self.log = logging.getLogger(cls_name)
        while self.log.hasHandlers():
            for i in self.log.handlers:
                self.log.removeHandler(i)
        self.fh = logging.FileHandler(filename=log_path, mode="a+",
                                      encoding="utf-8")
        self.formatter = logging.Formatter(
            fmt="[%(asctime)-15s %(name)s %(lineno)d %(process)d "
                "%(levelname)s] %(message)s",
            datefmt="%Y-%m-%d %H:%M:%S")
        self.fh.setFormatter(self.formatter)
        self.log.addHandler(self.fh)

    def info(self, msg, *args):
        self.log.setLevel(logging.INFO)
        self.log.info(msg, *args)

    def error(self, msg, *args):
        self.log.setLevel(logging.ERROR)
        self.log.error(msg, *args)

    def warnning(self, msg, *args):
        self.log.setLevel(logging.WARNING)
        self.log.warning(msg, *args)

    def debug(self, msg, *args):
        self.log.setLevel(logging.DEBUG)
        self.log.debug(msg, *args)

    def critical(self, msg, *args):
        self.log.setLevel(logging.CRITICAL)
        self.log.critical(msg, *args)
